//
//  DrinkViewController.m
//  安好
//
//  Created by lianchuang on 14-11-11.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "DrinkViewController.h"
#import "DrinkRemindCell.h"

#define drinkCellLineColor [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1];

@interface DrinkViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray *timeArr;
    NSArray *infoArr;
    NSArray *detailoArr;
}
@end

@implementation DrinkViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"喝水提醒设置";
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    
    
    if ([[user objectForKey:@"isSwithOn"] isEqual:[NSNull null]]||[user objectForKey:@"isSwithOn"]==nil) {
        NSString * isSwithOn = @"yes";
        [user setObject:isSwithOn forKey:@"isSwithOn"];
        RepeatReminderViewController *rrv = [[RepeatReminderViewController alloc]init];
        UISwitch *s = [[UISwitch alloc]init];
        s.on=YES;
        s.tag = 0;
        [rrv isSwitchChange:s];
    }

    
    //创建一个可变的数组长度为10
    self.clearArray = [[NSMutableArray array] init];
    
    //向数组中动态的添加对象,将需要作为用水提醒的时间放入可变数组
    [self.clearArray addObject:@"06:30"];
    [self.clearArray addObject:@"08:30"];
    [self.clearArray addObject:@"11:00"];
    [self.clearArray addObject:@"13:00"];
    [self.clearArray addObject:@"15:00"];
    [self.clearArray addObject:@"17:00"];
    [self.clearArray addObject:@"18:30"];
    [self.clearArray addObject:@"21:00"];
    
    //创建一个可变的数组长度为10
    self.numArray = [[NSMutableArray array] init];
    
    //向数组中动态的添加对象 作为标志位,给时间增加一个可以区别的ID号
    [self.numArray addObject:@"起床水"];
    [self.numArray addObject:@"早餐水"];
    [self.numArray addObject:@"休息水"];
    [self.numArray addObject:@"午餐水"];
    [self.numArray addObject:@"休息水"];
    [self.numArray addObject:@"下班水"];
    [self.numArray addObject:@"晚餐水"];
    [self.numArray addObject:@"睡前水"];
    
    
    [self makeNavBar];
    [self loadData];
    [self makeTableView];
    
}
#pragma mark - 导航条 配置
-(void)makeNavBar
{
    self.navigationItem.title = @"喝水提醒";
    //左按钮“返回”
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backClicked)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    //右按钮“设置”
    UIBarButtonItem *setBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_设置.png"] style:UIBarButtonItemStylePlain target:self action:@selector(setBtnClicked)];
    setBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = setBtnImg;
}
-(void)backClicked
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)setBtnClicked
{
    RepeatReminderViewController * rrv = [[RepeatReminderViewController alloc]initWithNibName:@"RepeatReminderViewController" bundle:nil];
    rrv.clearArray = self.clearArray;
    rrv.numArray=self.numArray;
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    [self.navigationController pushViewController:rrv animated:YES];

}
#pragma mark - 加载数据
-(void)loadData
{
    self.drinkDataArray = [NSMutableArray arrayWithCapacity:0];
    timeArr = @[@"06:30",@"08:30",@"11:00",@"13:00",@"15:00",@"17:00",@"18:30",@"21:00"];
    infoArr = @[@"清晨水，舒缓补水",@"早餐水，营养补充",@"加餐水，提神代谢",@"午餐水，促进消化",@"加餐水，解乏调节",@"下班水，舒缓降燥",@"晚餐水，调节肠胃",@"晚安水，澡后补水"];
    detailoArr = @[@"一天之计在于晨，快起来喝一杯蜂蜜水，唤醒你的身体和活力吧。",@"一天之计在于晨，快起来喝一杯蜂蜜水，唤醒你的身体和活力吧。",@"工作再繁忙，也不要忘了给身体加油，现在就拿起杯子补点水吧。",@"工作再繁忙，也不要忘了给身体加油，现在就拿起杯子补点水吧。",@"工作再繁忙，也不要忘了给身体加油，现在就拿起杯子补点水吧。",@"忙碌一天，倒杯水休息一下，生活不只是工作，身体健康更重要哦。",@"忙碌一天，倒杯水休息一下，生活不只是工作，身体健康更重要哦。",@"忙碌一天，倒杯水休息一下，生活不只是工作，身体健康更重要哦。"];

    for (int i = 0; i < timeArr.count; i++) {
        NSDictionary *dic = @{@"time":timeArr[i],@"info":infoArr[i]};
        [self.drinkDataArray addObject:dic];
    }

}
#pragma mark - 创建tableView 设置代理,添加头视图
-(void)makeTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-iOS7TopMargin) style:UITableViewStyleGrouped];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = self.view.backgroundColor;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //隐藏横向和竖向的滑条
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    //隐藏系统的cell之间的分割线
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    /**
     * table的头视图
     */
    
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenWidth/(16/5))];
    self.tableView.tableHeaderView = headerView;
    //添加table 头视图 -- 横幅
    self.headerImgView = [[UIImageView alloc] initWithFrame:headerView.bounds];
    [self.headerImgView setImage:[UIImage imageNamed:@"喝水提醒_banner.png"]];
    [headerView addSubview:self.headerImgView];
    
    /**
     * table的footer视图
     */
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 130)];
    self.tableView.tableFooterView = footerView;
    footerView.backgroundColor =self.view.backgroundColor;
    //添加底部视图
    self.footerTextLable = [[UILabel alloc]initWithFrame:CGRectMake(15, 15, kScreenWidth-25, 130) text:@"据世界组织统计，20%孩子生病，与喝水有关，30%老人有三高和动脉硬化等疾病，也与喝水有关，50%女人皮肤暗沉色斑也与喝水有关60%男性泌尿系统疾病，也与喝水有关！行动起来科学喝水，健康生活。" font:[UIFont systemFontOfSize:LABEL_FONT_SIZE_CONTENT_13]  textColor:TIME_COLOR backgroundColor:[UIColor clearColor] numberOfLines:0 lineSpacing:5];
    footerView.frame = CGRectMake(0, 0, kScreenWidth, self.footerTextLable.frame.size.height+15) ;
//    self.footerTextLable = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenWidth-25, 130)];
//    self.footerTextLable.backgroundColor = [UIColor clearColor];
//    self.footerTextLable.text = @"据世界组织统计，20%孩子生病，与喝水有关，30%老人有三高和动脉硬化等疾病，也与喝水有关，50%女人皮肤暗沉色斑也与喝水有关60%男性泌尿系统疾病，也与喝水有关！行动起来科学喝水，健康生活。";
//    self.footerTextLable.textAlignment = NSTextAlignmentLeft;
//    self.footerTextLable.textColor = [UIColor lightGrayColor];
//    self.footerTextLable.numberOfLines = 0;
//    self.footerTextLable.adjustsFontSizeToFitWidth = YES;
    [footerView addSubview:self.footerTextLable];
//    //设置 字体大小 和 行间距
//    [self attributeLable:self.footerTextLable andFontOfSize:12 andLineSpacing:6];
    
}
////lable文字的大小  和  行间距
//-(void)attributeLable:(UILabel*)textLable andFontOfSize:(CGFloat)fontSize andLineSpacing:(CGFloat)spacing
//{
//    textLable.font = [UIFont systemFontOfSize:fontSize];
//    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textLable.text];
//    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//    [paragraphStyle setLineSpacing:spacing];
//    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, textLable.text.length)];
//    textLable.attributedText = attributedString;
//}

#pragma mark - tableView的代理方法
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;

}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.drinkDataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return kScreenWidth == 414?44*1.2:44;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DrinkRemindCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        if (kScreenWidth == 414) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"DrinkRemindCell" owner:self options:nil] lastObject];

        }else{
        cell = [[[NSBundle mainBundle] loadNibNamed:@"DrinkRemindCell" owner:self options:nil] firstObject];
        }
    }
    
    cell.timeLable.text = [self.drinkDataArray[indexPath.row] objectForKey:@"time"];
    cell.infoLable.text = [self.drinkDataArray[indexPath.row] objectForKey:@"info"];
    /**  喝水提醒 开关
     *
     *  cell.remindSwitch
     */
    //头部 底部的分割线
    cell.topLineView.backgroundColor = drinkCellLineColor;
    cell.bottomLineView.backgroundColor = drinkCellLineColor;
    if (indexPath.row != 0) {
        cell.topLineView.hidden = YES;
    }else
    {
        cell.topLineView.frame = CGRectMake(0, 0, kScreenWidth, 0.5);
    }

    if (indexPath.row != self.drinkDataArray.count - 1)
    {
        cell.bottomLineView.frame = CGRectMake(15, (kScreenWidth == 414?44*1.2:44)-1, kScreenWidth-15-5, 0.5);
    }else
    {
        cell.bottomLineView.frame = CGRectMake(0,(kScreenWidth == 414?44*1.2:44)-1 , kScreenWidth, 0.5);
    }
    
    
    cell.remindSwitch.tag = indexPath.row;
    CGRect frame = cell.remindSwitch.frame;
    frame.origin.x = kScreenWidth - 51 - 15;
    cell.remindSwitch.frame = frame;
    
    NSString *isOn = [[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"switch%ld",(long)cell.remindSwitch.tag]];
    if ([isOn isEqualToString:@"yes"]) {
        cell.remindSwitch.on = YES;
    }else{
        cell.remindSwitch.on = NO;

    }
    [cell.remindSwitch addTarget:self action:@selector(switchAction:)forControlEvents:UIControlEventValueChanged];
    
    
    
    
    return cell;

}
// switch的代理 判断是否为on 如果是on就提醒喝水,off就将提醒移除
- (void)switchAction:(UISwitch *)stch
{
    self.remStr = [detailoArr objectAtIndex:stch.tag];
    
    NSString *timesStr = [timeArr objectAtIndex:stch.tag];
    
    AlarmClockRemind *al=[[AlarmClockRemind alloc]init];
    
    if (stch.on) {
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:[NSString stringWithFormat:@"switch%ld",(long)stch.tag]];
        
        [AlarmClockRemind cancelLocalNotificationMark:timesStr];
        
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"HH:mm:ss"];
        
        NSDate *fromDate =[formatter dateFromString:[AlarmClockRemind getTimeHHmmWithHHmmss:[self.clearArray objectAtIndex:stch.tag]]];
 
        
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"isSwithOn"] isEqualToString:@"no"]) {
            [al addLocalNotificationsWithTimes:fromDate isWorkingDays:NO category:@"喝水提醒" alertBody:self.remStr Mark:timesStr];
            
        }else{
            [al addLocalNotificationsWithTimes:fromDate isWorkingDays:YES category:@"喝水提醒" alertBody:self.remStr Mark:timesStr];
            
        }
        
        
        
        
    }else{
        [AlarmClockRemind cancelLocalNotificationMark:timesStr];
        [[NSUserDefaults standardUserDefaults] setObject:@"NO" forKey:[NSString stringWithFormat:@"switch%ld",(long)stch.tag]];
        
    }
    
}
#pragma mark - UIGestureRecognizerDelegate
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    if (self.navigationController.viewControllers.count == 1)//关闭主界面的右滑返回
    {
        return NO;
    }
    else
    {
        return YES;
    }
}

@end
